import { createRouter, createWebHashHistory } from "vue-router";
import { ElMessage } from 'element-plus'

import Login from '@/views/login/Login.vue'
import Home from "@/views/home/Home.vue";
import HomeCount from "@/views/home/children/HomeCount.vue";
import AccountList from "@/views/home/children/AccountList.vue";
import AccountAdd from "@/views/home/children/AccountAdd.vue";
import AccountEdit from "@/views/home/children/AccountEdit.vue";
import AccountCenter from "@/views/home/children/AccountCenter.vue";

import GoodsAdd from "@/views/home/children/GoodsAdd.vue";
import GoodsList from "@/views/home/children/GoodsList.vue";
import GoodsStatistics from "@/views/home/children/GoodsStatistics.vue";
import GoodsType from "@/views/home/children/GoodsType.vue";

import OrderList from "@/views/home/children/OrderList.vue";
import OrderStatistics from "@/views/home/children/OrderStatistics.vue";

import PermissionInfo from "@/views/home/children/PermissionInfo.vue";
import RolePermission from "@/views/home/children/RolePermission.vue";
import ShopInfo from "@/views/home/children/ShopInfo.vue";


const routes = [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
        path: '/home', component: Home,
        // meta:原信息
        meta: {
            taitle: '首页',
            unShow: true
        },
        children: [
            {
                path: '/home',
                meta: {
                    taitle: '首页',
                },
                component: HomeCount
            },
            // 商品修改
            {
                path: '/goodsEdit',
                component: () => import('@/views/home/children/GoodsEidit.vue')
            },
            {
                path:'/orderInfor',
                component:()=>import('@/views/home/children/OrderInfor.vue')
            }
        ]
    },
    //账号管理模块
    {
        path: '/account',
        component: Home,
        meta: {
            taitle: '用户管理'
        },
        redirect: '/accountList',
        children: [
            {
                path: '/accountList',
                meta: {
                    taitle: '账号列表'
                },
                component: AccountList
            },
            {
                path: '/accountAdd', meta: {
                    taitle: '添加账号'
                }, component: AccountAdd
            },
            {
                path: '/accountEdit', meta: {
                    taitle: '修改密码'
                }, component: AccountEdit
            },
            {
                path: '/accountCenter', meta: {
                    taitle: '个人中心'
                }, component: AccountCenter
            },
        ]
    },
    //商品管理
    {
        path: '/goods',
        component: Home,
        redirect: '/goodsList',
        meta: {
            taitle: '商品管理'
        },
        children: [
            {
                path: '/goodsList',
                meta: {
                    taitle: '商品列表'
                },
                component: GoodsList,
            },
            {
                path: '/goodsAdd',
                meta: {
                    taitle: '商品添加'
                },
                component: GoodsAdd
            },
            {
                path: '/goodsType',
                meta: {
                    taitle: '商品分类'
                },
                component: GoodsType
            }
        ]
    },
    //订单管理
    {
        path: '/order',
        component: Home,
        redirect: '/orderList',
        meta: {
            taitle: '订单管理',
            unShow: true
        },
        children: [
            {
                path: '/orderList',
                component: OrderList,
                meta: {
                    taitle: '订单管理'
                }
            }
        ]
    },
    //店铺管理
    {
        path: '/shop',
        component: Home,
        redirect: '/shopInfo',
        meta: {
            taitle: '店铺管理',
            unShow: true //只有一级的就在面包屑显式一次
        },
        children: [
            {
                path: '/shopInfo',
                component: ShopInfo,
                meta: {
                    taitle: '店铺管理'
                }
            }
        ]
    },
    //统计数据
    {
        path: '/tatistics',
        component: Home,
        redirect: '/goodsStatistics',
        meta: {
            taitle: '统计数据'
        },
        children: [
            {
                path: '/goodsStatistics',
                component: GoodsStatistics,
                meta: {
                    taitle: '商品统计'
                }
            },
            {
                path: '/orderStatistics',
                component: OrderStatistics,
                meta: {
                    taitle: '订单统计'
                }
            }
        ]
    },
    //权限管理
    {
        path: '/permission',
        component: Home,
        redirect: '/permissionInfo',
        meta: {
            taitle: '权限管理'
        },
        children: [
            {
                path: '/permissionInfo',
                component: PermissionInfo,
                meta: {
                    taitle: '权限管理'
                }
            },
            {
                path: '/rolePermission',
                component: RolePermission,
                meta: {
                    taitle: '角色管理'
                }
            }
        ]
    },
    // 404页面配置
    {
        path:'/:pathMatch(.*)*',//匹配所有路径 上面路由访问不到就会走这里
        component: ()=>import('@/views/error/Error.vue')
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

//路由前置守卫
//再路由跳转之前执行
router.beforeEach((to, from) => {
    //to 从哪里来
    //from 去哪里
    if(to.path === '/login'){
        return true
    }
    if(!sessionStorage.getItem('user')){
        ElMessage.error('请先登录')
        return '/login' //如果没有登录就跳转到登录页面
    }
})

export default router;